package com.huaxin.device.mapping;

import com.huaxin.device.dto.enums.AlarmLevel;
import com.huaxin.device.dto.enums.AlarmType;
import com.huaxin.device.dto.request.AlarmThresholdRuleCreateReqDTO;
import com.huaxin.device.dto.request.AlarmThresholdRuleUpdateReqDTO;
import com.huaxin.device.models.AlarmThresholdRule;
import com.huaxin.hxenum.enums.ComparisonOperator;
import com.huaxin.hxenum.enums.Unit;
import org.mapstruct.AfterMapping;
import org.mapstruct.Mapper;
import org.mapstruct.Mapping;
import org.mapstruct.MappingTarget;

@Mapper(componentModel = "spring")
public interface AlarmThresholdRuleMapping {

    @Mapping(target = "alarmType", ignore = true)
    @Mapping(target = "comparisonOp", ignore = true)
    @Mapping(target = "unit", ignore = true)
    @Mapping(target = "alarmLevel", ignore = true)
    AlarmThresholdRule toEntity(AlarmThresholdRuleCreateReqDTO in);

    @AfterMapping
    default void afterMapping(AlarmThresholdRuleCreateReqDTO in, @MappingTarget AlarmThresholdRule entity) {
        entity.setAlarmType(AlarmType.of(in.getAlarmType()));
        entity.setComparisonOp(ComparisonOperator.of(in.getComparisonOp()));
        entity.setUnit(Unit.of(in.getUnit()));
        entity.setAlarmLevel(AlarmLevel.of(in.getAlarmLevel()));
    }

    @Mapping(target = "alarmType", ignore = true)
    @Mapping(target = "comparisonOp", ignore = true)
    @Mapping(target = "unit", ignore = true)
    @Mapping(target = "alarmLevel", ignore = true)
    void updateEntity(AlarmThresholdRuleUpdateReqDTO in, @MappingTarget AlarmThresholdRule entity);

    @AfterMapping
    default void afterMapping(AlarmThresholdRuleUpdateReqDTO in, @MappingTarget AlarmThresholdRule entity) {
        entity.setAlarmType(AlarmType.of(in.getAlarmType()));
        entity.setComparisonOp(ComparisonOperator.of(in.getComparisonOp()));
        entity.setUnit(Unit.of(in.getUnit()));
        entity.setAlarmLevel(AlarmLevel.of(in.getAlarmLevel()));
    }
}